//
//  UIView+EWTErrorPage.m
//  EWTBase 通用错误页 = 图片 + 文字 + 动作按钮
//
//  Created by iMAC_HYH on 2018/8/18.
//

#import "UIView+EWTErrorPage.h"
#import <objc/runtime.h>
#import "Masonry.h"
#import "UIImage+Helper.h"

@interface _EWTErrorPage : UIView

@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, copy) NSString *actionTitle;
@property (nonatomic, copy) typeof(void(^)(void)) actionHandler;

- (void)showActionButton;
- (void)hideActionButton;

@end

@interface UIView (_EWTErrorPage)

@property (nonatomic, strong) _EWTErrorPage *errorPage;

@end

@implementation UIView (EWTErrorPage)

- (void)showBlankPage:(NSString * _Nonnull)title {
    [self showErrorPage:title
                  image:[UIImage imageNamed:@"blank_page"]
            actionTitle:nil
          actionHandler:nil];
}

- (void)showErrorPage:(NSString * _Nonnull)title actionHandler:(nullable void(^)(void))actionHandler {
    
    [self showErrorPage:title
                  image:[UIImage imageNamed:@"error_page"]
            actionTitle:@"重新加载"
          actionHandler:actionHandler];
}

- (void)showErrorPage:(NSString * _Nonnull)title
                image:(UIImage * _Nonnull)image
          actionTitle:(nullable NSString *)actionTitle
        actionHandler:(nullable void(^)(void))actionHandler {
    if (!self.errorPage) {
        self.errorPage = [_EWTErrorPage new];
    }
    self.errorPage.title = title.copy;
    self.errorPage.image = image;
    self.errorPage.actionTitle = actionTitle.copy;
    self.errorPage.actionHandler = [actionHandler copy];
    if (actionHandler) {
        [self.errorPage showActionButton];
    } else {
        [self.errorPage hideActionButton];
    }
    // add error page
    [self addSubview:self.errorPage];
    [self.errorPage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
        make.width.height.equalTo(self);
    }];
}

- (void)hideErrorPage {
    if (self.errorPage) {
        [self.errorPage removeFromSuperview];
        self.errorPage = nil;
    }
}

- (_EWTErrorPage *)errorPage {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setErrorPage:(_EWTErrorPage *)errorPage {
    objc_setAssociatedObject(self, @selector(errorPage), errorPage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

#pragma mark - _EWTErrorPage

@implementation _EWTErrorPage {
    UIImageView *_imageView;
    UILabel *_titleLabel;
    UIButton *_actionButton;
    UILabel *_actionTitleLabel;
}

- (instancetype)init {
    if (self = [super init]) {
        [self createUI];
    }
    return self;
}

- (void)createUI {
    self.backgroundColor = UIColorFromRGB(0xF8F9FC, 1.0);
    // contentPanel
    UIView *contentView = [UIView new];
    contentView.backgroundColor = [UIColor clearColor];
    // image
    _imageView = [UIImageView new];
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    //
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.numberOfLines = 0;
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.font = [UIFont systemFontOfSize:14];
    _titleLabel.textColor = UIColorFromRGB(0xA7ACB9,1.0);
    //
    _actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _actionButton.layer.cornerRadius = 16;
    _actionButton.layer.masksToBounds = YES;
    
    [_actionButton setBackgroundImage:[UIImage imageWithColor:UIColorFromRGB(0x0387FF,1.0)] forState:UIControlStateNormal];
    [_actionButton addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    //
    _actionTitleLabel = [[UILabel alloc] init];
    _actionTitleLabel.textAlignment = NSTextAlignmentCenter;
    _actionTitleLabel.textColor = [UIColor whiteColor];
    _actionTitleLabel.font = [UIFont systemFontOfSize:16];
    _actionTitleLabel.text = @"重新加载"; // default value
    
    // add views
    [self addSubview:contentView];
    [contentView addSubview:_imageView];
    [contentView addSubview:_titleLabel];
    [contentView addSubview:_actionButton];
    [_actionButton addSubview:_actionTitleLabel];
    
    // layout
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self).offset(15);
        make.right.equalTo(self).offset(-15);
    }];
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentView);
        make.centerX.mas_equalTo(contentView);
        make.height.mas_equalTo(80);
    }];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self->_imageView.mas_bottom).offset(20);
        make.left.right.equalTo(contentView);
    }];
    [_actionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self->_titleLabel.mas_bottom).offset(20);
        make.bottom.equalTo(contentView).offset(-30);
        make.centerX.mas_equalTo(contentView);
        make.height.mas_equalTo(32);
    }];
    [_actionTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self->_actionButton);
        make.left.equalTo(self->_actionButton.mas_left).offset(20);
        make.right.equalTo(self->_actionButton.mas_right).offset(-20);
    }];
}

- (void)buttonClick {
    [self.superview hideErrorPage];
    if (self.actionHandler) {
        self.actionHandler();
    }
}

- (void)showActionButton {
    _actionButton.hidden = NO;
}

- (void)hideActionButton {
    _actionButton.hidden = YES;
}

#pragma mark - Getters and Setters

- (void)setTitle:(NSString *)title {
    _title = title.copy;
    _titleLabel.text = _title;
}

- (void)setImage:(UIImage *)image {
    _image = image;
    _imageView.image = _image;
}

- (void)setActionTitle:(NSString *)actionTitle {
    _actionTitle = actionTitle.copy;
    _actionTitleLabel.text = _actionTitle;
}

static UIImage * EUIImageFromColor(UIColor *color) {
    CGRect rect = CGRectMake(0.0, 0.0, 1.0, 1.0);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [color setFill];
    UIRectFill(rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end

